package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.mapper.NewsMapper;
import com.maiworld.mapper.TbNewtableMapper;
import com.maiworld.mapper.TbSealSchoolMapper;
import com.maiworld.mapper.TbStudentMapper;
import com.maiworld.pojo.News;
import com.maiworld.pojo.TbNewtable;
import com.maiworld.pojo.TbNewtableExample;
import com.maiworld.pojo.TbNewtableExample.Criteria;
import com.maiworld.pojo.TbSealSchool;
import com.maiworld.seller.service.NewtableService;
import com.mysql.fabric.xmlrpc.base.Array;

import entity.PageResult;

@Service
public class NewtableServiceImpl implements NewtableService {

	@Autowired
	private TbNewtableMapper tbNewtableMapper;
	
	@Autowired
	private TbSealSchoolMapper tbSealSchoolMapper;
	
	@Autowired
	private TbStudentMapper tbStudentMapper;
	
	@Autowired
	private NewsMapper newsMapper;
	
	//获取合同
	@Override
	public Map<String, Object> findOneNewsId(Long id) {	
		HashMap<String, Object> result = new HashMap<String, Object>();		
		
		TbNewtable table = tbNewtableMapper.selectByPrimaryKey(id);
		List<String> array = new ArrayList<String>();
		array.add(table.getSignimgurl());

		if(array.isEmpty()){
			result.put("drive", null);
		}else {
			result.put("drive", array);
		}
		result.put("statusCode", 200);
		result.put("message", "成功");	
		return result;
	}
	//返回某个车类型的报名价格信息表
	@Override
	public Map<String, Object> findAll(String status, String type) {
		Map<String,Object> result = new HashMap<String, Object>();
		
		List<TbNewtable> array = new ArrayList<TbNewtable>();		
		TbNewtable newT = new TbNewtable();
		newT.setType(type);
		newT.setStatus(status);	
		array = tbNewtableMapper.selectByType(newT);

		result.put("classArr", array);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	}

	//获取某一个班型的详细介绍 id:是班型的主键
	@Override
	public Map<String, Object> findOne(Long sid, String status, Long id, String type) {
		Map<String,Object> result = new HashMap<String, Object>();
		
		List<TbNewtable> array = new ArrayList<TbNewtable>();		
		TbNewtable newT = new TbNewtable();		
		newT.setId(id);
		array = tbNewtableMapper.selectByType(newT);
		
		Long num = tbNewtableMapper.selectNewId(id);		
		TbNewtable res = array.get(0);		
		
		res.setNumber(num);

		TbSealSchool school = new TbSealSchool();
		school.setStatus(status);
		school = tbSealSchoolMapper.selectByPrimaryKey(Long.parseLong(status));		
	
		res.setAddress(school.getAddress());
		
		
		result.put("classArr", res);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	
	}

	//获取首页信息包括：轮播图、驾校简介、班型介绍
	@Override
	public Map<String, Object> HomePageInformation(String status, String openid, Long sid) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		
		List<News> news = newsMapper.selectByClassId(31L, status);
		Map<String, Object> res = new HashMap<String,Object>();
		res.put("id",news.get(0).getId());
		res.put("content",news.get(0).getContent());
		
		List<String> imgarr = new ArrayList<String>();
		if(news.get(0).getImgUrl1() != null){
			imgarr.add(news.get(0).getImgUrl1());
		}
		if(news.get(0).getImgUrl2() != null){
			imgarr.add(news.get(0).getImgUrl2());
		}
		if(news.get(0).getImgUrl3() != null){
			imgarr.add(news.get(0).getImgUrl3());
		}
		res.put("bannerUrl", imgarr);
		result.put("info", res);
		
		//获取班型介绍
		List<TbNewtable> NewtableList = new ArrayList<>();
		List<TbNewtable> array = new ArrayList<>();
		array = tbNewtableMapper.selectByExample(null);
		//Map<String, Object> map = new HashMap<String, Object>();
		for (TbNewtable tbNewtable : array) {
			TbNewtable tbNewtable2 = new TbNewtable();
			tbNewtable2.setId(tbNewtable.getId());
			tbNewtable2.setBname(tbNewtable.getBname());
			tbNewtable2.setType(tbNewtable.getType());
			tbNewtable2.setMode(tbNewtable.getMode());
			tbNewtable2.setCarNumber(tbNewtable.getCarNumber());
			tbNewtable2.setOldprice(tbNewtable.getOldprice());
			tbNewtable2.setPrice(tbNewtable.getPrice());
			tbNewtable2.setVehicleType(tbNewtable.getVehicleType());
			NewtableList.add(tbNewtable2);

		}
		result.put("classification", NewtableList);
		
		//驾校简介
		List<TbSealSchool> schoolList = new ArrayList<>();
		List<TbSealSchool> arrayList = new ArrayList<>();
		arrayList = tbSealSchoolMapper.selectByExample(null);
		for (TbSealSchool tbSealSchool : arrayList) {
			TbSealSchool tbSealSchool2 = new TbSealSchool();
			tbSealSchool2.setId(tbSealSchool.getId());
			tbSealSchool2.setName(tbSealSchool.getName());
			schoolList.add(tbSealSchool2);
		}
		result.put("schools", schoolList);
		
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
		
		
		
	}

	@Override
	public List<TbNewtable> findAll() {
		// TODO Auto-generated method stub
		return tbNewtableMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<TbNewtable> page = (Page<TbNewtable>) tbNewtableMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbNewtable tbNewtable) {
		// TODO Auto-generated method stub
		tbNewtableMapper.insert(tbNewtable);
	}

	@Override
	public void update(TbNewtable tbNewtable) {
		// TODO Auto-generated method stub
		tbNewtableMapper.updateByPrimaryKey(tbNewtable);
	}

	@Override
	public TbNewtable findOne(Long id) {
		// TODO Auto-generated method stub
		return tbNewtableMapper.selectByPrimaryKey(id);
		
	}

	@Override
	public void delete(Long[] ids) {
		// TODO Auto-generated method stub
		for(Long id:ids){
			tbNewtableMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public PageResult findPageNewtable(TbNewtable tbNewtable, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		TbNewtableExample example = new TbNewtableExample();
		Criteria create = example.createCriteria();
		if(create!=null){
			if(tbNewtable.getStatus()!=null && tbNewtable.getStatus().length()>0){
				create.andStatusEqualTo(tbNewtable.getStatus());
			}
		}
		Page<TbNewtable> page = (Page<TbNewtable>) tbNewtableMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	@Override
	public TbNewtable selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return tbNewtableMapper.selectByPrimaryKey(id);
	}

/*	//上传签约的图片，sid:用户主键
	@Override
	public Map<String, Object> addNewtable(Map<String, String> params,Long sid) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		

		TbStudent student = new TbStudent();		
		student = tbStudentMapper.selectByPrimaryKey(sid);
		student.getNewId();
		

		TbNewtable tbnew = new TbNewtable();
		tbNewtableMapper.selectByPrimaryKey(Long.parseLong("student.getNewId()"));
		tbnew.setSignimgurl(params.get("signimgurl"));
		tbNewtableMapper.updateByPrimaryKeySelective(tbnew);
		
		result.put("statusCode", 200);
		result.put("message", "请求成功");	
		return result;
	}
*/

}
